﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Serialization;

namespace MutFormStudio {
	public class BaseStudio : IEnumerable<StudioFormHead> {
		/// <summary>只有基础studio拥有这个表</summary>
		public readonly List<Studio> studios = new List<Studio>();
		internal BaseStudio() { }

		/// <summary> 控件关闭时，将触发此事件</summary>
		internal event EventHandler<ControlsCloseEventArgs> eControlsClosing;
		internal void onControlsClosing(ControlsCloseEventArgs e) {
			eControlsClosing?.Invoke(this, e);
		}

		/// <summary>关闭其中一个控件</summary>
		/// <param name="head_to_close">一个即将关闭的控件</param>
		internal void closeOneControl(StudioFormHead head_to_close) {
			ControlsCloseEventArgs e = new ControlsCloseEventArgs();
			e.Close_flags = new ClosingControlFlag[] { new ClosingControlFlag(head_to_close) };
			onControlsClosing(e);
			if (e.Cancle_all == false) {
				var flag = e.Close_flags[0];
				if (flag.Cancle == false) {
					flag.head.tag_studio.removeControl(flag.head);
					flag.head.Dispose();
					flag.dControlClosed?.Invoke(flag.Control);
				}
			}
		}

		internal StudioFormHead dragEnter(object sender, DragEventArgs e) {
			try {
				if (e.Data.GetDataPresent(typeof(StudioFormHead))) {
					StudioFormHead sf_head = e.Data.GetData(typeof(StudioFormHead)) as StudioFormHead;
					if(sf_head.tag_studio.Base_studio == this) {
						e.Effect = DragDropEffects.Move;
						return sf_head;
					} else {
						Console.WriteLine($"来自外部工作间的控件，不接受拖放");
					}
				} else {
					e.Effect = DragDropEffects.None;
					Console.WriteLine($"拖放来的不是StudioFormHead，不接受拖放");
				}
				return null;
			} catch (System.InvalidCastException) {
				e.Effect = DragDropEffects.None;
				Console.WriteLine($"{sender}拖放错误");
				return null;
			}





		}




		public IEnumerator<StudioFormHead> GetEnumerator() {
			foreach (var studio in studios) {
				foreach (var target in studio) {
					yield return target;
				}
			}
		}
		IEnumerator IEnumerable.GetEnumerator() {
			throw new NotImplementedException();
		}
	}
}
